package preferences.Database;

import preferences.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DatabasePostgres implements DBInterface{
    
    String dbms;
    String host;
    int port;
    String dbname;
    String username;
    String password;
    Connection conn=null;
    public DatabasePostgres(String dbms, String host,String port, String dbname, String username, char[] password) {
        this.dbms = dbms;
        this.host = host;
        this.port = Integer.parseInt(port);
        this.dbname = dbname;
        this.username = username;
        if(password!=null)
            this.password = new String(password);
    }

    public String init() {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
            return "Couldn't find the driver!";
        }
        try {
            conn = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+dbname, username, password);
        } catch (SQLException se) {
            se.printStackTrace();
            return "Couldn't connect :" + se.getMessage();
        }
        return null;
    }

    public Connection getConn() {
        return conn;
    }



    @Override
    public String toString() {
        return  "DBMS : "+dbms+"\n "+
                "Host : "+host+"\n "+
                "Port : "+port+"\n "+
                "Database Name : "+dbname+"\n "+
                "Username : "+username+"\n "+
                "Password : "+password+"\n ";
    }

    public List<String> getTables() {
        List<String> tables = new ArrayList<String>();
        try{
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select tablename from pg_tables where schemaname='public'");
            while(rs.next()){
                tables.add(rs.getString(1));
            }
            rs.close();
            stmt.close();
        }catch(SQLException sqle){
            sqle.printStackTrace();
            return null;
        }
        return tables;
    }

    public List<Column> getColumns(String table){
        List<Column> cols = new ArrayList<Column>();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from "+table);
            ResultSetMetaData rsmd = rs.getMetaData();
            for(int i=1;i<=rsmd.getColumnCount();i++)
                cols.add(new Column(rsmd.getColumnName(i), rsmd.getColumnTypeName(i)));
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
        return cols;
    }

    public List<String[]> getResults(String table,String where){
        List<String[]> results=new ArrayList<String[]>();
        try{
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM "+table+ " WHERE "+where );
            ResultSetMetaData rsmd = rs.getMetaData();
            int n=rsmd.getColumnCount();
            String[] md=new String[n];
            for(int i=1;i<=n;i++)
                md[i-1]=rsmd.getColumnName(i);
            results.add(md);
            while(rs.next()){
                String[] tmp = new String[n];
                for(int i=1;i<=n;i++){
                    tmp[i-1]=rs.getString(i);
                }
                results.add(tmp);
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        return results;
    }

}